﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using BusinessLogic;
using BusinessObject;
using Utility;
using System.Resources;
namespace WebTeamplate
{
    public partial class Admin_ManageCategory : BasePage
    {
        #region Fields
        public string urlDetail = "CategoryDetail.aspx?id={0}";
        public string urlNew = "CategoryDetail.aspx";
        #endregion

        #region ViewState
        public int PageIndex
        {

            get
            {

                object o = ViewState["PageIndex"];

                if (o != null) return (int)o;

                return 0;

                //a default

            }

            set { ViewState["PageIndex"] = value; }

        }
        public string SortField
        {

            get
            {
                object o = ViewState["SortField"];
                if (o != null) return (string)o;
                return string.Empty;
            }

            set { ViewState["SortField"] = value; }

        }
        public string SortExpression
        {

            get
            {
                object o = ViewState["SortExpression"];
                if (o != null) return (string)o;
                return string.Empty;
            }

            set { ViewState["SortExpression"] = value; }

        }
        #endregion

        #region Init,Load
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                InitData();
                LoadData();
            }

        }
        protected override void OnInit(EventArgs e)
        {
            base.OnInit(e);
            this.imgCategoryIDSortDown.ImageUrl = CommonFunction.DEFAULT_SORTDOWN_IMAGEURL;
            this.imgCategoryIDSortUp.ImageUrl = CommonFunction.DEFAULT_SORTUP_IMAGEURL;
            this.imgCategoryNameSortDown.ImageUrl = CommonFunction.DEFAULT_SORTDOWN_IMAGEURL;
            this.imgCategoryNameSortUp.ImageUrl = CommonFunction.DEFAULT_SORTUP_IMAGEURL;
            imgCategoryIDSortUp.Click += new ImageClickEventHandler(imgCategoryIDSortUp_Click);
            imgCategoryIDSortDown.Click += new ImageClickEventHandler(imgCategoryIDSortDown_Click);
            imgCategoryNameSortDown.Click += new ImageClickEventHandler(imgCategoryNameSortDown_Click);
            imgCategoryNameSortUp.Click += new ImageClickEventHandler(imgCategoryNameSortUp_Click);
        }
        #endregion

        #region Methods
       
        private void InitData()
        {
            this.PageIndex = 1;
            Pager1.PageSize = CommonFunction.DEFAULT_ADMIN_PAGE_SIZE;
            CommonFunction.InitStatusPublishForRadioButtonList(rblStatus);
        }        
        public void LoadData()
        {
            hidListId.Value = string.Empty;
            hidListIdSelect.Value = string.Empty;
            Category objCate = new Category();
            objCate.Condition = " where 1=1 ";
            if (txtCategoryId.Text != string.Empty)
                objCate.Condition += " and categoryId='" + txtCategoryId.Text + "'";
            //objCate.Condition = "where 1=1  and categoryId='00'";
            if (txtCategoryName.Text != string.Empty)
                objCate.Condition += " and categoryName like N'%" + txtCategoryName.Text + "%' ";
            if(rblStatus.SelectedValue!=string.Empty)
                objCate.Condition += " and IsPublished=" + rblStatus.SelectedValue + " ";
            objCate.ObjectSortOrder.SortExpression=this.SortExpression;
            objCate.ObjectSortOrder.SortField = this.SortField;
            objCate.ObjectPaging.PageIndex = this.PageIndex;
            objCate.ObjectPaging.PageSize = Pager1.PageSize;
            int count = 0;
            List<CategoryResult> lstResult = CategoryUtil.SearchCategorySomeCondition(objCate);
            rptData.DataSource = lstResult;
            rptData.DataBind();
            if (lstResult != null && lstResult.Count > 0)
                count = lstResult.Count;
            Pager1.SetPaging(this.PageIndex, count, objCate.ToltalRow);
        }       
        private void Delete(string id)
        {
            if (CategoryUtil.Delete(id))
                LoadData();

        }
        public void Publish(string id)
        {
            Category obj = new Category();
            obj.CategoryId = id;
            obj.LastUpdatedDate = CommonFunction.Now;
            obj.LastUpdatedBy = this.userLoginName;
            if (CategoryUtil.Publish(obj))
                LoadData();
        }
        

        #endregion

        #region Events,Handler
        protected void rptData_ItemDataBound(object sender, RepeaterItemEventArgs e)
        {
            if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
            {
                CategoryResult cate = (CategoryResult)e.Item.DataItem;
                if (cate == null)
                    return;
                Literal ltr = (Literal)e.Item.FindControl("ltrNo");
                ltr.Text = (e.Item.ItemIndex + 1).ToString();
                ltr = (Literal)e.Item.FindControl("ltrId");
                ltr.Text = cate.CategoryId;
                ltr = (Literal)e.Item.FindControl("ltrName");
                ltr.Text = "<a href='CategoryDetail.aspx?id=" + cate.CategoryId + "'>" + Server.HtmlEncode(cate.CategoryName) + "</a>";
                ImageButton btnPublish = (ImageButton)e.Item.FindControl("btnPublish");
                btnPublish.CommandArgument = cate.CategoryId;
                if (cate.IsPublished)
                    btnPublish.ImageUrl = CommonFunction.DEFAULT_ACTIVE_IMAGEURL;
                else
                    btnPublish.ImageUrl = CommonFunction.DEFAULT_DEACTIVE_IMAGEURL;
                HyperLink hplEdit = (HyperLink)e.Item.FindControl("hplEdit");
                hplEdit.NavigateUrl = string.Format(this.urlDetail, cate.CategoryId);
                LinkButton lbtnDelete = (LinkButton)e.Item.FindControl("lbtnDelete");
                lbtnDelete.Text = Resources.Resource.LABEL_DELETE;
                lbtnDelete.CommandArgument = cate.CategoryId;
                hidListId.Value += cate.CategoryId + ";";
            }
        }
        protected void rptData_ItemCommand(object source, RepeaterCommandEventArgs e)
        {
            string id = e.CommandArgument.ToString();
            switch (e.CommandName)
            {
                case "publish":
                    {
                        Publish(id);
                        break;
                    }
                case "delete":
                    {
                        Delete(id);
                        break;
                    }
            }

        }
        protected void btnSearch_Click(object sender, EventArgs e)
        {
            this.PageIndex = 1;
            LoadData();
        }
        void imgCategoryNameSortUp_Click(object sender, ImageClickEventArgs e)
        {
            this.SortField = "categoryName";
            this.SortExpression = "DESC";
            LoadData();
        }
        void imgCategoryNameSortDown_Click(object sender, ImageClickEventArgs e)
        {
            this.SortField = "categoryName";
            this.SortExpression = "ASC";
            LoadData();
        }
        void imgCategoryIDSortDown_Click(object sender, ImageClickEventArgs e)
        {
            this.SortField = "categoryId";
            this.SortExpression = "ASC";
            LoadData();
        }
        void imgCategoryIDSortUp_Click(object sender, ImageClickEventArgs e)
        {
            this.SortField = "categoryId";
            this.SortExpression = "DESC";
            LoadData();
        }
        protected void Pager1_PageIndexChanging(object obj, PagerControl.PageIndexChangeEventArgs e)
        {
            this.PageIndex = e.newPageIndex;
            LoadData();
        }
        protected void Pager1_PageSizeChanging(object obj, PagerControl.PageSizeChangeEventArgs e)
        {
            this.PageIndex = 1;
           // this.PageSize = e.newPageSize;
            LoadData();
        }
        protected void btnAddNew_Click(object sender, EventArgs e)
        {
            Response.Redirect(urlNew);
        }
        protected void btnDelete_Click(object sender, EventArgs e)
        {
            List<Category> lstCate = new List<Category>();
            string[] arr = hidListIdSelect.Value.Split(';');
            for (int i = 0; i < arr.Length; i++)
            {
                if (arr[i].ToString() != string.Empty)
                {
                  
                    Delete(arr[i].ToString());
                }
            }
            //CategoryUtil.Delete(lstCate);
            LoadData();
        }
        #endregion
    }
}
